<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use Illuminate\Http\Request;

class CategoryController extends Controller
{

    function __construct()
    {
        //这里要设置一个中间件验证请求是否有权限
//        这里之所以没有使用$this->middleware('auth:admin') 是因为这样虽然也能进行guard守卫是admin的验证但是当验证不通过的时候会自动跳转到route('login')这个路由,但是我这边没有设置login路由
        $this->middleware('admin');
    }

    public function index(Category $category)
    {
        $categorys = $category->getCategoryTree();
        return view('admin.category.index',compact('categorys'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Category $category)
    {
        $categorys = $category->getCategoryTree();
        return view('admin.category.create',compact('categorys'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
     Category::create($request->all());
     return redirect(route('admin.category.create'))->with('success','栏目创建成功');

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function show(Category $category)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function edit(Category $category)
    {
        //通过调用getAllCategory()获取整理过后的栏目数组
        $formatCategoryTree = $category->getAllCategory();
        //加载栏目编辑模板
        return view('admin.category.edit',compact('formatCategoryTree','category'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Category $category)
    {
        //$category是update之前的对象模型,调用之前的category对象的update方法更新这个模型对象.
        //使用update()更新的时候要给update传递一个数组
        $category->update($request->all());
        return redirect(route('admin.category.index'))->with('success','更新栏目成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function destroy(Category $category)
    {
        //首先判断当前传递过来的要删除的栏目是否有子栏目,如果有子栏目就不能够删除当前栏目
        if($category->hasChild()){
            return back()->with('error','有子栏目,不能删除');
        }else{
            $category->delete();
            return redirect(route('admin.category.index'))->with('success','删除栏目成功');
        }
    }
//    获取当前栏目子栏目的方法
    function getSonCategory($id){
        $categorys = Category::where('parent_id',$id)->get();
        return $categorys;
    }
}
